<?php

class Skill extends Frank implements iFrank {

    //Properties
    protected $description;
    protected $level;
    protected $familyId;
    protected $groupId;

    //Getters
    public function description() {
        return $this->description;
    }

    public function level() {
        return $this->level;
    }

    public function familyId() {
        return $this->familyId;
    }

    public function groupId() {
        return $this->groupId;
    }

    public function __construct($id, $name, $description, $level, $familyId, $groupId) {
        $this->id = $id;
        $this->name = $name;
        $this->description = $description;
        $this->level = $level;
        $this->familyId = $familyId;
        $this->groupId = $groupId;
    }

    public static function getObject($row) {
        return new Skill($row["id"], $row["name"], $row["description"], $row["level"], $row["family_id"], $row["group_id"]);
    }

    public static function getAll($toArray = false, $fields = null, $query = null, $db = null) {
        $query = 'select * from `' . SKILL_TABLE . '`';
        return parent::getAll($toArray, $fields, $query, $db);
    }

    public static function getById($id, $toArray = false, $fields = null, $query = null, $db = null) {
        $query = 'select * from `' . SKILL_TABLE . '` where id = ' . $id;
        return parent::getById($id, $toArray, $fields, $query, $db);
    }

    public static function getAllOrderedByFamilyAndGroup($toArray = false, $fields = null, $query = null, $db = null) {
        $myOrderedSkills = null;

        $mysqli = self::mysqliConnect();
        $mySkills = Skill::getAll(false, null, null, $mysqli);
        $myFamilies = SkillFamily::getAll(false, null, null, $mysqli);
        $myGroups = SkillGroup::getAll(false, null, null, $mysqli);

        if ($mySkills != null && $myGroups != null) {
            $myOrderedSkills = array();
            $mySkillsLentgh = 0;
            foreach ($myGroups as $group) {
                $mySkillsLentgh = array_push($myOrderedSkills, array('group' => $group, 'familyTab' => array()));
                $emptyFamilyIndex = 0;
                if ($myFamilies != null) {
                    foreach ($myFamilies as $family) {
                        if ($family->groupId() == $group->id())
                            array_push($myOrderedSkills[$mySkillsLentgh - 1]['familyTab'], array('family' => $family, 'skills' => array()));
                    }
                }
                $emptyFamilyIndex = array_push($myOrderedSkills[$mySkillsLentgh - 1]['familyTab'], array('family' => new SkillFamily(), 'skills' => array()));
                foreach ($mySkills as $skill) {
                    if ($skill->groupId == $group->id()) {
                        $eduExp = $skill->getEducationExperience($mysqli);
                        if ($skill->familyId == null)
                            array_push($myOrderedSkills[$mySkillsLentgh - 1]['familyTab'][$emptyFamilyIndex - 1]['skills'], array('skill' => $skill, 'education' => $eduExp['education'], 'experience' => $eduExp['experience']));
                        else {
                            $famIndex = 0;
                            foreach ($myOrderedSkills[$mySkillsLentgh - 1]['familyTab'] as $currFamily) {
                                if ($currFamily['family']->id() == $skill->familyId) {
                                    array_push($myOrderedSkills[$mySkillsLentgh - 1]['familyTab'][$famIndex]['skills'], array('skill' => $skill, 'education' => $eduExp['education'], 'experience' => $eduExp['experience']));
                                    break;
                                }
                                $famIndex++;
                            }
                        }
                    }
                }
            }
        }

        $mysqli->close();
        return $myOrderedSkills;
    }

    public static function getByIdOrderedByFamilyAndGroup($id, $toArray = false, $fields = null, $query = null, $db = null) {
        $query = 'select * '
                . 'from  `' . SKILL_TABLE . '` '
                . 'left join `' . SKILL_FAMILY_TABLE . '` ON `' . SKILL_TABLE . '`.family_id = `' . SKILL_FAMILY_TABLE . '`.family_id '
                . 'inner join `' . SKILL_GROUP_TABLE . '` ON `' . SKILL_TABLE . '`.group_id = `' . SKILL_GROUP_TABLE . '`.group_id '
                . 'where `' . SKILL_TABLE . '`.id = ' . $id;

        $results = self::getFromQuery($query);
        if ($results != null) {
            $group = SkillGroup::getObject($results[0]);
            $skill = Skill::getObject($results[0]);
            if ($skill->familyId == null)
                $family = new SkillFamily();
            else
                $family = SkillFamily::getObject($results[0]);
            $myOrderedSkills[] = array('group' => $group, 'familyTab' => array(array('family' => $family, 'skills' => array($skill))));
            return $myOrderedSkills;
            //return self::tabObjectsToArray($myOrderedSkills, $toArray, $fields);
        }
        return null;
    }

    private static function tabObjectsToArray($myCategories, $toArray = false, $fields = null) {
        if ($toArray) {
            if (isset($myCategories) && count($myCategories) > 0) {
                $myCategoriesTab = array();
                foreach ($myCategories as $category) {
                    $categoryTab = $category['category']->toArray($fields);
                    $interestsTab = array();
                    foreach ($category['interests'] as $interest) {
                        array_push($interestsTab, $interest->toArray($fields));
                    }
                    $categoryTab['interests'] = $interestsTab;
                    array_push($myCategoriesTab, $categoryTab);
                }
                return $myCategoriesTab;
            }
        }
        return $myCategories;
    }

    private function getEducationExperience($db) {
        $eduExp = array();
        $eduExp['education'] = Education::getAllForSkillId($this->id(), $db);
        $eduExp['experience'] = Experience::getAllForSkillId($this->id(), $db);
        return $eduExp;
    }

}

?>